package cqgans.app.sunao.com.cqgans.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.SimpleDateFormat;
import java.util.List;

import cqgans.app.sunao.com.cqgans.bean.UserPurchase;
import cqgans.app.sunao.com.cqgansnew.R;

public class PurchaseAdapter extends BaseQuickAdapter<UserPurchase, BaseViewHolder> {

    public PurchaseAdapter(@Nullable List<UserPurchase> data) {
        super(R.layout.item_purchase_list, data);
        Log.i("StringAAA", "原：哈    " + "脱敏后：" + encodeAddr("哈"));
        Log.i("StringAAA", "原：哈哈    " + "脱敏后：" + encodeAddr("哈哈"));
        Log.i("StringAAA", "原：哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈哈哈哈哈"));
        Log.i("StringAAA", "原：哈哈哈哈哈哈哈哈哈哈哈    " + "脱敏后：" + encodeAddr("哈哈哈哈哈哈哈哈哈哈哈"));
    }

    @Override
    protected void convert(BaseViewHolder helper, UserPurchase item) {
        helper.setText(R.id.user_no_tv, item.userNo)
                .setText(R.id.user_name_tv, item.userType == 1 ? encodeAddr(item.userName) : encodeName(item.userName))
                .setText(R.id.user_time_tv, item.insured == 1 ? "截止至：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.insuredExpiredTime) : "")
                .setText(R.id.user_addr_tv, encodeAddr(item.userAddress))
                .setText(R.id.user_type_tv, item.userType == 1 ? "工商" : "居民")
                .setText(R.id.user_status_tv, item.insured == 1 ? "已购买" : "未购买")
                .setBackgroundRes(R.id.user_type_tv, item.userType == 1 ? R.color.red : R.color.colorPrimary)
                .setBackgroundRes(R.id.user_status_tv, item.insured == 1 ? R.drawable.bg_login_btn : R.drawable.gray_corner_bg);
        helper.addOnClickListener(R.id.root_ll);
    }

    public static String encodeName(String name) {
        if(TextUtils.isEmpty(name)) {
            return  "";
        }
        if(name.length() == 1) {
            return name;
        } else {
            String str = name.substring(0, 1);
            for(int i = 1; i < name.length(); i ++) {
                str += "*";
            }
            return str;
        }

    }

    public static String encodeAddr(String name) {
        if(TextUtils.isEmpty(name)) {
            return  "";
        } else if(name.length() == 1) {
            return name;
        } else if(name.length() == 2) {
            return name.substring(0, 1) + "*";
        } else if(name.length() == 3) {
            return name.substring(0, 2) + "*";
        } else if(name.length() == 4) {
            return name.substring(0, 2) + "**";
        } else if(name.length() == 5) {
            return name.substring(0, 2) + "***";
        } else if(name.length() == 6) {
            return name.substring(0, 3) + "***";
        }  else if(name.length() < 10){
            String pre = name.substring(0, 3);
            String last = name.substring(name.length() - 3, name.length());
            for(int i = 3; i < name.length() - 3; i ++) {
                pre += "*";
            }
            return pre + last;
        } else {
            String pre = name.substring(0, 4);
            String last = name.substring(name.length() - 4, name.length());
            for(int i = 4; i < name.length() - 4; i ++) {
                pre += "*";
            }
            return pre + last;
        }
    }

}
